<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Promise实例</title>
</head>
<body>

</body>
<script type="text/javascript">
  window.onload=function () {
      function getAjax(url) {
          return new Promise((resolve,reject)=>{
              const xhr = new XMLHttpRequest();
              xhr.onreadystatechange=()=>{
                  if(xhr.readyState===4){
                      if(xhr.status===200){
                         let data =  JSON.parse(xhr.responseText);
                         resolve(data);
                      }else {
                          reject("error");
                      }
                  }
              }
              xhr.open("get",url);
              xhr.send();
          })
      };
      getAjax("http://localhost:3000/news?id=1")
          .then((data)=>{
             return getAjax("http://localhost:3000"+data.commentsUrl)
           },(error)=>{
                console.log(error)
           })
          .then((data)=>{
                console.log(data)
           },(error)=>{
                console.log(error)
           });
  }
</script>

</html>